"""
异步线程记录日志
"""
import threading
import queue
import logging
import time
import random
import common.constants as const

class LogThread(threading.Thread):
    contents:queue.Queue = None

    logging.basicConfig(level=logging.INFO, format='%(asctime)s %(threadName)s %(message)s',
                        datefmt='%Y%m%d %H:%M:%S', filemode='a')

    @classmethod
    def getInstance(cls):
        if not hasattr(cls, '_instance'):
            cls._instance = LogThread()
        return cls._instance
     

    def __init__(self):
        threading.Thread.__init__(self)
        self.contents = queue.Queue()

    def setContent(self, content):
        self.contents.put(content)

    def run(self):
        print("Start LogThread>>>>>>>>>>>>>>>>>>>>")
        while True:
            self.printLog()
            time.sleep(1)


    def printLog(self):
        while self.contents is None or self.contents.qsize() == 0:
            if random.random() == 0.5 or True:
                print("log thread is running......")
                time.sleep(2)

            time.sleep(5)
            continue

        while self.contents.qsize() > 0:
            content = self.contents.get()
            logging.info(content)   

        print("LogEnd.")
        time.sleep(1)